package com.urchin.avatarhero.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

/**********************************************************************
AvatarHero - A task management RPG Android Application
Copyright (C) 2013  Peter Clay

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

public class SQLiteHandler extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "AHdata.db";
	private static final int DATABASE_VERSION = 2;
	private SQLiteDatabase db;
	private Context dispcontext;
		
	// Constructor
	public SQLiteHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		dispcontext = context;
	}

	// Note, this is not called until the database is opened
	@Override
	public void onCreate(SQLiteDatabase db) {
		Toast.makeText(dispcontext,"Creating new quest database...",Toast.LENGTH_SHORT).show();
		String createQuery = "";
		// Create Item Table
		createQuery = "CREATE TABLE items (_id integer primary key, name, desc, count);";
		db.execSQL(createQuery);
			// Need to populate the item table - TODO
		// Create a table for Quests
		// Note: compflag = 0 for unassigned quest, 1 for a completed quest, -1 for a failed quest
		// diff is the difficult assigned to the quest
		// exp is the experience earned (initially 0 until completed)
		// item is the item found from the quest (if any)
		createQuery = "CREATE TABLE pq (_id integer primary key autoincrement, calid integer, eventid integer, instanceid integer, diff integer DEFAULT 0, exp integer DEFAULT 0, item integer DEFAULT -1, compflag integer DEFAULT 0);";
		db.execSQL(createQuery);
	}
	
	@Override
	public void onOpen(SQLiteDatabase db) {

	}

	// General use functions
	
	public void opendb() throws SQLException {
		// Open database in R/W mode
		this.db = this.getWritableDatabase();
        //Toast.makeText(dispcontext,"Database is open",Toast.LENGTH_SHORT).show();
	}	
	
	public void closedb() {
		if(this.db.isOpen()) {
			this.db.close();
	        //Toast.makeText(dispcontext,"Database is closed",Toast.LENGTH_SHORT).show();
		}
	}
	
	// User functions
	
	public void deleteAllQuests() {
		Toast.makeText(dispcontext,"Deleting all Quests!",Toast.LENGTH_LONG).show();
		db.delete("pq", null, null);		// Deletes all rows
	}
	
	public void insertQuest(int calid, int eventid, int instid, int diff, int flag) {
		ContentValues newCon = new ContentValues();
		newCon.put("calid", calid);
		newCon.put("eventid",  eventid);
		newCon.put("instanceid", instid);
		newCon.put("diff", diff);
		newCon.put("compflag", flag);
		db.insert("pq", null, newCon);
	}
	
	public void updateQuest(int calid, int eventid, int instid, int diff, int exp, int item, int flag) {
		ContentValues editCon = new ContentValues();
		editCon.put("calid", calid);
		editCon.put("eventid",  eventid);
		editCon.put("instanceid", instid);
		editCon.put("diff", diff);
		editCon.put("exp", exp);
		editCon.put("item", item);
		editCon.put("compflag", flag);
		db.update("pq",  editCon,  "calid = ? AND eventid = ? AND instanceid = ?",new String[] {String.valueOf(calid), String.valueOf(eventid), String.valueOf(instid)});
	}
	
	public void deleteQuest(long id) {
		db.delete("pq",  "_id=" + id,  null);
	}
	
	public Cursor getAllCompletedQuests() {
		return db.query("pq", new String [] {"calid", "eventid", "instanceid"}, "compflag = 1", null, null, null, "eventid" );		// Events not complete or failed, and have a difficulty
	}
	
	public Cursor getAllFailedQuests() {
		return db.query("pq", new String [] {"calid", "eventid", "instanceid"}, "compflag=-1", null, null, null, "eventid");
	}
	
	public Cursor getCurQuests() {
		return db.query("pq", new String [] {"calid", "eventid", "instanceid", "diff"}, "compflag = 0 AND diff > 0", null, null, null, "eventid" );		// Events not complete or failed, and have a difficulty
	}
	
	public Cursor getAllQuests() {
		return db.query("pq", new String [] {"calid", "eventid", "instanceid"}, "diff > 0", null, null, null, "eventid" );		// All events in database (should have diff >= 0)
	}
	
	public Cursor getAllExp() {
		return db.query("pq", new String [] {"exp"}, null, null, null, null, "exp");
	}
	
	public Cursor getQuest(int calid, int eventid, int instid) {
		Cursor rcur = db.rawQuery("SELECT calid,eventid,instanceid,diff,compflag FROM pq WHERE calid = ? AND " +
				"eventid = ? AND instanceid = ?", new 
				String[] {String.valueOf(calid), String.valueOf(eventid), 
				String.valueOf(instid) } );
		
		return rcur;
	}
	
	public Cursor getQuestId(int calid, int eventid, int instid) {
		return db.query("pq", new String [] {"id"}, "calid = ? AND eventid = ? AND instanceid = ?", new String[] {String.valueOf(calid), String.valueOf(eventid), String.valueOf(instid)}, null, null, "eventid" );		// All events in database (should have diff >= 0)
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
}
